//
//  NetworkTool.swift
//  testSwift
//
//  Created by taoge on 2018/3/28.
//  Copyright © 2018年 FT. All rights reserved.
//

import Alamofire
import SwiftyJSON
protocol NetworkToolProtocol {
    static  func loadNewTitleData(compleTionHander :@escaping(_ newsTitles:[HomeNewsTitle])->())
}
// 延伸
extension NetworkToolProtocol {
   
    static  func loadNewTitleData(compleTionHander :@escaping(_ newsTitles:[HomeNewsTitle])->()){
        let url = Base_URL + "/article/category/get_subscribed/v1/?"
        let parmas = ["device_id" : device_id,"iid": iid]
       
        Alamofire.request(url, parameters: parmas).responseJSON{
            (response) in
//         网络错误提示信息
      
            guard response.result.isSuccess else {return}
            if let value = response.result.value{
                      print(value)
                let json = JSON(value)
                guard json["message"] == "success" else {return}
              //===========================    方法 1
/*
                 let arr = json["data"]["data"].array
                 var dataA = [HomeNewsTitle]()

                    for dic in arr!{
                        let model = HomeNewsTitle()
                        model.name = dic["name"].string ?? ""
                        model.web_url = dic["web_url"].string ?? ""
                        dataA.append(model)
                 }
                compleTionHander(dataA)
 */
               //===========================     方法 2
                if let dataDict = json["data"].dictionary {
                    if let datas = dataDict["data"]!.arrayObject {
                      compleTionHander(datas.flatMap({ HomeNewsTitle.deserialize(from: $0 as? Dictionary) }))
                    }
                }
                
            }
        }
        
    }
    
    
    
}
struct NetworlTool :NetworkToolProtocol {
    
}
